#include "mybutton.h"

MyButton::MyButton(QWidget *parent) : QPushButton(parent)
{
    srand(time(0));
    windowRect = parent->geometry();
    wheelDistance = 0;
    this->setMouseTracking(true);
}

void MyButton::enterEvent(QEvent *event)
{
    qDebug() << "Mouse EnterEvent";
    (void)event;

    //真正实现 “点不到” 按钮的效果
//    int x = rand() % windowRect.width();
//    int y = rand() % windowRect.height();

//    this->setGeometry(x,y,this->width(),this->height());

}

void MyButton::leaveEvent(QEvent *event)
{
    (void)event;
    qDebug() << "Mouse LeaveEvent";
    qDebug() << "wheelDistance: " << wheelDistance;
}

void MyButton::mouseDoubleClickEvent(QMouseEvent *event)
{
    (void)event;
    qDebug() << "Mouse DoubleClickEvent";
}

//要触发这个，需先设置允许鼠标追踪setMouseTracking(true)
void MyButton::mouseMoveEvent(QMouseEvent *event)
{
    (void)event;
    qDebug() << "Mouse MoveEvent: " << event->x() << " " << event->y();
}

void MyButton::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        qDebug() << "Mouse LeftButton PressEvent";
    }
    else if(event->button() == Qt::RightButton)
    {
        qDebug() << "Mouse RightButton PressEvent";
    }
    else if(event->button() == Qt::MiddleButton)
    {
        qDebug() << "Mouse MiddleButton PressEvent";
    }
}

void MyButton::mouseReleaseEvent(QMouseEvent *event)
{
    (void)event;
    qDebug() << "Mouse ReleaseEvent";
}

void MyButton::wheelEvent(QWheelEvent *event)       //只在该函数中观察的话也可以用一个静态局部变量
{
    //qDebug() << event->delta();
    wheelDistance += event->delta();
    qDebug() << wheelDistance;
}

//void MyButton::keyPressEvent(QKeyEvent *event)
//{
//    if(event->key() == Qt::Key::Key_A)
//    {
//        qDebug() << "MyButton push Key_A";
//    }
//}
